package com.solution.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.solution.constant.LoginConstants;
import com.solution.constant.SystemVariableConstants;
import com.solution.delegate.SystemVariableServiceDelegate;
import com.solution.exception.DelegateException;
import com.solution.exception.ServiceBeanException;
import com.solution.form.SystemVariableForm;
import com.solution.model.SystemVariable;
import com.solution.model.UserInfo;
import com.solution.util.SearchVO;

public class SystemVariableAction extends Action{

	/**
	 * logging instance.
	 */
	private Logger logger = Logger.getLogger(SystemVariableAction.class.getName());
	
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,
		HttpServletResponse response) throws IOException, ServletException {
		logger.debug("Entering :: QuestionAction:execute()");
		SystemVariableForm systemVariableForm =(SystemVariableForm)form;
		ActionMessages errors = new ActionMessages() ;
		try{
			HttpSession session = request.getSession();
			UserInfo userInfo = null;
			if(session != null){
				userInfo = (UserInfo)session.getAttribute(LoginConstants.SESSION_USER_INFO);
			}
			if("/saveSystemVariables".equals(mapping.getPath())){
				return saveSystemVariables(mapping, systemVariableForm, request, response);
			}
			else{
				SystemVariableServiceDelegate systemVariableServiceDelegate = new SystemVariableServiceDelegate();
				SearchVO searchVO = new SearchVO();
				searchVO.addEqualsCondition("clientType.id", userInfo.getClientType().getId());
				List<SystemVariable> systemVariables = systemVariableServiceDelegate.get(searchVO);
				systemVariableForm.setSystemVariableList(systemVariables);				
				systemVariableForm.setScreen_type("Edit");
			}
		}
		catch(ServiceBeanException sbe){
			logger.error(sbe);
			errors.add("error", new ActionMessage("SystemVariable.fail.exception", sbe.getMessage()));
			systemVariableForm.setScreen_type("Edit");
		}
		catch(DelegateException de){
			logger.error(de);
			errors.add("error", new ActionMessage("SystemVariable.add.fail"));
			systemVariableForm.setScreen_type("Edit");
		}
		return mapping.findForward("success");
	}
	
	public ActionForward saveSystemVariables(ActionMapping mapping,SystemVariableForm systemVariableForm,HttpServletRequest request, 
		HttpServletResponse response) throws IOException, ServletException {
		ActionMessages errors = new ActionMessages() ;
		try{
			SystemVariableServiceDelegate systemVariableServiceDelegate = new SystemVariableServiceDelegate();
			HttpSession session = request.getSession();
			UserInfo userInfo = null;
			if(session != null){
				userInfo = (UserInfo)session.getAttribute(LoginConstants.SESSION_USER_INFO);
			}
			SearchVO searchVO = new SearchVO();
			searchVO.addEqualsCondition("clientType.id", userInfo.getClientType().getId());
			List<SystemVariable> systemVariables = systemVariableServiceDelegate.get(searchVO);
			Map<String,SystemVariable> mapSysVariable = new HashMap<String, SystemVariable>();
			for(SystemVariable tempSystem:systemVariables){
				mapSysVariable.put(tempSystem.getVariableName(), tempSystem);
			}
			SystemVariable systemVariable = null;
			Date modifiedOn = new Date();
			for(int i =0; i<systemVariableForm.getVariableName().length;i++){
				systemVariable = mapSysVariable.get(systemVariableForm.getVariableName()[i]);
				systemVariable.setVariableValue(systemVariableForm.getVariableValue()[i]);
				systemVariable.setModifiedBy(userInfo.getUsername());
				systemVariable.setModifiedOn(modifiedOn);
				//systemVariableServiceDelegate.updateSystemVariables(systemVariables);
			}
			//List<SystemVariable> systemVariables = new ArrayList<SystemVariable>();
			
						
			systemVariableServiceDelegate.updateSystemVariables(systemVariables);
			systemVariableForm.setSystemVariableList(systemVariables);
			systemVariableForm.setScreen_type("View");
		}
		catch(ServiceBeanException sbe){
			logger.error(sbe);
			errors.add("error", new ActionMessage("SystemVariable.fail.exception", sbe.getMessage()));
			systemVariableForm.setScreen_type("Edit");
		}
		catch(DelegateException de){
			logger.error(de);
			errors.add("error", new ActionMessage("SystemVariable.add.fail"));
			systemVariableForm.setScreen_type("Edit");
		}
		return mapping.findForward("success");
	}
}
